package cn.wolfcode.luowowo.mgrsite.web.listener;

import cn.wolfcode.luowowo.article.query.AnswerQuery;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.AnswerSatisSortVo;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 容器启动并初始化之后再执行
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyRedisService strategyRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IAnswerRedisService answerRedisService;

    @Reference
    private ITravelRedisService travelRedisService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IAnswerService answerService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        AnswerQuery answerQuery = new AnswerQuery();
        System.out.println("===========执行初始化操作=======");
        for (int a = 1; a <= 3; a++) {
            answerQuery.setCondition(a);
            for (int b = 1; b <= 3; b++) {
                answerQuery.setRange(b);
                List<AnswerSatisSortVo> vos = answerService.querySort(answerQuery);
                switch (a){
                    case AnswerQuery.CONDITION_GOLD:answerRedisService.setZsetGoldByDateType(b,vos);break;
                    case AnswerQuery.CONDITION_ANSWER:answerRedisService.setZsetAnswerByDateType(b,vos);break;
                    case AnswerQuery.CONDITION_THUMB:answerRedisService.setZsetThumbByDateType(b,vos);break;
                }
            }
        }


//List<StrategyDetail> strategyDetails = strategyDetailService.list();
//        for (StrategyDetail detail : strategyDetails) {
//            StrategyStatisVO statisVo = new StrategyStatisVO();
//            BeanUtil.copyProperties(statisVo,detail);
//            statisVo.setDestId(detail.getDest().getId());
//            statisVo.setDestName(detail.getDest().getName());
//            statisVo.setStrategyId(detail.getId());
//            strategyRedisService.setStatisVo(statisVo);
//            strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefix(),detail.getId(),0);
//            strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(),detail.getId(),0);
//
//        }
//        List<Travel> travels = travelService.list();
//        for (Travel travel : travels) {
//            TravelStatisVO vo = new TravelStatisVO();
//            BeanUtil.copyProperties(vo,travel);
//            vo.setDestId(travel.getDest().getId());
//            vo.setDestName(travel.getDest().getName());
//            vo.setTravelId(travel.getId());
//            travelRedisService.setStatisVo(vo);
//
//        }
        System.out.println("-----------------已执行数据初始化操作--------------------");
    }
}
